package dal

import (
	"yingsheng.com/go-api-server/example/model"
	"yingsheng.com/go-api-server/orm"
)

type TestDA struct {
	orm.DA
}

func NewTestDA() (*TestDA, error) {
	da := &TestDA{}
	err := orm.InitDA(da, &model.TestMD{})

	return da, err
}

func (da *TestDA) UpdateByUserId(userId int64) (int64, error) {
	return da.Where("UserId=?", userId).Update()
}

func (da *TestDA) ListLikeUserName(index, size int, userName string) (ml *orm.ModelList, err error) {
	userName = "%" + userName + "%"
	da.Where("UserName like ?", userName).Desc("CreateTime")
	return da.list(index, size)
}

func (da *TestDA) ListBetweenCreateTime(index, size int, startTime, endTime string) (ml *orm.ModelList, err error) {
	da.Where("CreateTime>?", startTime).And("CreateTime<?", endTime).Desc("CreateTime")
	return da.list(index, size)
}

func (da *TestDA) ListAll(index, size int) (ml *orm.ModelList, err error) {
	da.Desc("CreateTime")
	return da.list(index, size)
}

func (da *TestDA) list(index, size int) (ml *orm.ModelList, err error) {
	da.ModelList = &orm.ModelList{
		PageIndex: index,
		PageSize:  size,
		Items:     new([]model.TestMD),
	}
	return da.GetModelList()
}
